<template>
  <t-dialog
    :header="header"
    :visible.sync="visible"
    :onClose="closeDialog"
    :closeOnOverlayClick="false"
    @confirm="onSubmit"
    :confirmBtn="{
        content: loading?'提交中...':'确认',
        theme: 'primary',
        loading: loading,
      }"
    width="50%"
    top="5%"
  >
    <t-form ref="form" :colon="true"  :data="formData" labelWidth="100px" :rules="rules">
      <t-form-item label="上级成果" name="parentId">
        <t-tree-select clearable filterable :treeProps="{keys: {value: 'id'}}" v-model="formData.parentId" :data="menuOptions" placeholder="没有上级专业" />
      </t-form-item>
      <t-form-item label="成果名称" name="name">
        <t-input clearable v-model="formData.name"  placeholder="请输入成果名称" />
      </t-form-item>
      <t-form-item label="显示排序" name="sort">
        <t-input-number clearable v-model="formData.sort" :min="1" placeholder="请输入" />
      </t-form-item>
    </t-form>
  </t-dialog>
</template>

<script>
export default {
  name: "add-edit-result",
  props: ['header', 'objId', 'parentId'],
  data(){
    return {
      rules: {
        // parentId: [
        //   { required: true, message: "上级专业不能为空" }
        // ],
        name: [
          { required: true, message: "成果名称不能为空" }
        ],
        code:[
          { required: true, message: "专业代码不能为空" }
        ],
        sort: [
          { required: true, message: "显示排序不能为空" }
        ],
      },
      menuOptions: [],
      visible: true,
      loading: false,
      formData:{
        parentId: null,
        name: "",
        code:'',
        sort: 1,
      }
    }
  },
  watch: {
    objId: {
      immediate: true,
      handler(val) {
        if (val) {
          this.getObj();
        }
      }
    },
    parentId: {
      immediate: true,
      handler(val) {
        if (val) {
          this.formData.parentId = val;
        }
      }
    }
  },
  created() {
    if (!this.objId){
      this.getTreeselect();
    }
  },
  methods:{
    getObj(){
      this.$api.system.result.treeselect().then(response => {
        this.menuOptions = [];
        this.menuOptions = response.data;
        this.$api.system.result.get(this.objId).then(response => {
          if (response.data.parentId === 0){
            response.data.parentId = null;
          }
          this.$nextTick(() => {
            this.formData = response.data;
          });
        }).catch((e) => {
          this.$message.error(e.toString());
        });
      }).catch((e) => {
        this.$message.error(e.toString());
      });

    },
    onSubmit() {
      this.$refs.form.validate().then((validateResult) => {
        if (validateResult === true) {
          if(!this.formData.parentId){
            this.formData.parentId = 0;
          }
          this.loading = true;
          if (this.objId) {
            let {parentId} = this.formData;
            if(this.objId === this.formData.parentId){
              parentId = 0;
            }
            const row = {...this.formData,parentId}
            this.$api.system.result.edit(row).then(() => {
              this.$message.success("修改成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          } else {
            let {parentId} = this.formData;
            if(!this.formData.parentId){
              parentId = 0;
            }
            const row = {...this.formData,parentId}
            this.$api.system.result.add(row).then((response) => {
              this.$message.success("新增成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          }
        }
      });
    },
    closeDialog() {
      this.$emit("update:visible", false);
    },
    /** 查询菜单下拉树结构 */
    getTreeselect() {
      this.$api.system.result.treeselect().then(response => {
        this.menuOptions = [];
        this.menuOptions = response.data;
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
  }
}
</script>

<style scoped>
.overlay-options {
  display: inline-block;
  font-size: 20px;
}
</style>
